package com.future.mayi.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.adapter.GoodsBatchAdapter;
import com.future.mayi.adapter.GoodsBatchOrderAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.GoodsBatchModel;
import com.future.mayi.model.GoodsMultiPostfeeModel;
import com.future.mayi.model.SpecModel;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.PriceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewGoodsBatchOrderActivity extends BaseTitleActivity {


    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_main_title)
    TextView tvMainTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private List<GoodsBatchModel> list;
    private String goodsId, title, img;
    private GoodsBatchOrderAdapter adapter;

    public static void startActivity(Activity context, String goodsId, String title, String img, ArrayList<GoodsBatchModel> list, int requestCode) {
        Intent intent = new Intent(context, NewGoodsBatchOrderActivity.class);
        intent.putExtra("goodsId", goodsId);
        intent.putExtra("title", title);
        intent.putExtra("img", img);
        intent.putExtra("list", list);
        context.startActivityForResult(intent, requestCode);
    }


    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("批量确认订单");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_new_goods_batch_order);
    }

    @Override
    protected void initView() {
        adapter = new GoodsBatchOrderAdapter(mContext);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    }

    private double money = 0;

    @Override
    protected void initDates() {
        goodsId = getIntent().getStringExtra("goodsId");
        title = getIntent().getStringExtra("title");
        img = getIntent().getStringExtra("img");
        list = (List<GoodsBatchModel>) getIntent().getSerializableExtra("list");
        tvMainTitle.setText(title);
        HCUtils.loadWebImg(mContext, ivImg, img);


        for (GoodsBatchModel model : list) {
            double itemMoney = 0;
            for (SpecModel specModel : model.getSelectList()) {
                itemMoney = itemMoney + PriceUtils.getMultiply(specModel.getNum(), specModel.getGoods_price());
            }
            model.setMoney(itemMoney);
            money = money + itemMoney + (model.getPostage() == -1 ? 0 : model.getPostage());
        }
        tvPrice.setText(PriceUtils.getPriceText2(money));
        tvOrderNum.setText("共" + list.size() + "单");

        adapter.setList(list);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.tv_enter)
    public void onClick() {
        tvEnter.setEnabled(false);
        List<GoodsMultiPostfeeModel> allItem = new ArrayList<>(list.size());
        for (GoodsBatchModel model : list) {
            allItem.add(model.getGoodsMultiPostfeeModel());
        }
        PayActivity.startActivity(this, goodsId, App.getGson().toJson(allItem), money);
        tvEnter.setEnabled(true);
    }

}
